﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data.Linq;

namespace MvcApplication1
{
    /// <summary>
    /// 数据库建立工厂
    /// Created By : 张占岭
    /// Created Date：2011-10-14
    /// Modify By:
    /// Modify Date:
    /// Modify Reason:
    /// </summary>
    internal static class DbFactory
    {
        #region Fields
        static System.Timers.Timer sysTimer;
        volatile static Dictionary<Thread, DataContext[]> divDataContext;
        #endregion

        #region Constructors
        static DbFactory()
        {
            divDataContext = new Dictionary<Thread, DataContext[]>();
            sysTimer = new System.Timers.Timer(10000);
            sysTimer.AutoReset = true;
            sysTimer.Enabled = true;
            sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
            sysTimer.Start();

         }
        #endregion
       
        #region Private Methods
        /// <summary>
        /// 清理DbContext上下文
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            List<Thread> list = divDataContext.Keys
                                              .Where(item => item.ThreadState == ThreadState.Stopped)
                                              .ToList();
            if (list != null && list.Count > 0)
            {
                foreach (var thread in list)
                {
                    foreach (var context in divDataContext[thread])
                    {
                        if (context != null)
                            context.Dispose();
                    }
                }
            }
        }
        #endregion

        #region Public Methods

        /// <summary>
        /// 手动清除数据上下文，根据线程
        /// </summary>
        /// <param name="thread"></param>
        public static void ClearContextByThread(Thread thread)
        {
            divDataContext.Remove(thread);
        }

        /// <summary>
        /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
        /// </summary>
        /// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
        /// <returns>LINQ数据库连接对象</returns>
        public static DataContext Intance(string dbName)
        {
            return Intance(dbName, Thread.CurrentThread);
        }

        /// <summary>
        /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
        /// </summary>
        /// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
        /// <param name="thread">当前线程引用的对象</param>
        /// <returns>LINQ数据库连接对象</returns>
        public static DataContext Intance(string dbName, Thread thread)
        {

            if (!divDataContext.Keys.Contains(thread))
            {
                divDataContext.Add(thread, new DataContext[1]);
            }

            if (dbName.Equals("test"))
            {
                if (divDataContext[thread][0] == null)
                {
                    divDataContext[thread][0] = new MvcApplication1.DataClasses1DataContext();
                }
                return divDataContext[thread][0];
            }


            return null;

        }
        #endregion

    }
}
